#include "TFBasicInfoHandler.h"
#include <tfbondlib/BondFuture.h>
#include <bondlib/CCFlowInfo.h>
#include <QJsonArray>
#include <QJsonObject>

bool TFBasicInfoHandler::processCmd(const QJsonObject& cmdObj, const ReqInfo& reqinfo, bool unReg)
{
	parseCondition(cmdObj);

	CFutureArray futureArray = CBondFuture::instance().GetFutureArray();
	CCCFlowManager& ccFlow = CCCFlowManager::instance();

	int count = 0;
	QJsonObject jsRes;
	QJsonArray jsList;
	for (auto iter = futureArray.m_data.begin(); iter != futureArray.m_data.end(); ++iter)
	{
		if(!m_AllTf && m_TfCodeList.find(iter->m_TF_ID) == m_TfCodeList.end())
			continue;

		QJsonObject jsItem;
		if (m_ColMarket)			jsItem["market"] = (int)iter->m_dwMarket;
		if (m_ColKey)				jsItem["key"] = iter->m_TF_Key;
		if (m_ColId)				jsItem["id"] = iter->m_TF_ID;
		if (m_ColShortName)			jsItem["shortname"] = iter->m_TF_ShortName;
		if (m_ColDeliveryMonth)		jsItem["deliverymonth"] = iter->m_Delivery_Month;
		if (m_ColLastTradeDay)		jsItem["lasttradeday"] = (int)iter->m_Last_Trading_Day;
		if (m_ColDeliveryDate)		jsItem["deliverydate"] = (int)iter->m_Delivery_Date;
		if (m_ColBondTerm)			jsItem["bondterm"] = (int)iter->m_BondTerm;
		if (m_ColDeliMatuStart)		jsItem["deliMatustart"] = (int)iter->m_Deliverable_maturity_Start;
		if (m_ColDeliMatuEnd)		jsItem["deliMatuend"] = (int)iter->m_Deliverable_maturity_End;
		if (m_ColQuoteWay)			jsItem["quoteway"] = iter->m_Quoted_Way;
		if (m_ColMinChange)			jsItem["minchange"] = iter->m_Min_Change;
		if (m_ColTradeTime)			jsItem["tradetime"] = iter->m_Trading_Time;
		if (m_ColLastDayTradeTime)	jsItem["lastdaytratime"] = iter->m_Last_Day_Trading_Time;
		if (m_ColPricelimit)		jsItem["pricelimit"] = iter->m_Price_Limit;
		if (m_ColLowestMarg)		jsItem["lowestmarg"] = iter->m_Lowest_Margins;
		if (m_ColDeliverWay)		jsItem["deliverway"] = iter->m_Delivery_Way;
		if (m_ColListedMarket)		jsItem["listedmarket"] = iter->m_Listed_Market;
		if (m_ColIsTradable)		jsItem["istradable"] = iter->m_Is_Tradable;
		if (m_ColType)				jsItem["type"] = iter->m_type;
		if (m_ColMain10Y)			jsItem["main10y"] = 0;
		if (m_ColMain5Y)			jsItem["main5y"] = 0;

		if (m_ColMain10Y || m_ColMain5Y)
		{
			std::lock_guard<std::mutex> locker(ccFlow.GetMutex());
			const CCCFlowInfo* pInfo = ccFlow.GetCCFlowInfo("T0001");
			if (pInfo && strcmp(iter->m_TF_ID, pInfo->GetCurrentTFID()) == 0)
			{
				if (m_ColMain10Y)			jsItem["main10y"] = 1;
			}
			else
			{
				pInfo = ccFlow.GetCCFlowInfo("TF0001");
				if (pInfo && strcmp(iter->m_TF_ID, pInfo->GetCurrentTFID()) == 0)
				{
					if (m_ColMain5Y)			jsItem["main5y"] = 1;
				}
			}
		}

		jsList.append(jsItem);
		if ((++count) % 20 == 0)
		{
			jsRes["finish"] = 0;
			jsRes["list"] = jsList;
			QJsonDocument doc(jsRes);
            emit sendDataToWeb(reqinfo.m_frameId, reqinfo.m_sid, doc.toJson(QJsonDocument::Compact), reqinfo.m_jsCb);

			QJsonObject emptyObj;
			QJsonArray emptyArr;
			jsRes.swap(emptyObj);
			jsList.swap(emptyArr);
		}
	}

	jsRes["finish"] = 1;
	jsRes["list"] = jsList;
	QJsonDocument doc(jsRes);
    emit sendDataToWeb(reqinfo.m_frameId, reqinfo.m_sid, doc.toJson(QJsonDocument::Compact), reqinfo.m_jsCb);
	return true;
}

void TFBasicInfoHandler::parseCondition(const QJsonObject& cmdObj)
{
	QJsonObject condObj = cmdObj["cond"].toObject();
	if (condObj.isEmpty())
		return;
	//TODO
}
